package com.xone.xapp;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class SplashScreenActivity extends Activity {
	
	protected String TAG = "SplashScreenActivity";

	// used to know if the back button was pressed in the splash screen activity
	// and avoid opening the next activity

	// Splash screen timer
	private static int SPLASH_TIME_OUT = 500;
	
	protected AutoLoginDataTask task = null;
	protected String username = null;
	protected String password = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splashscreen);
		//当前的手机所使用的User-Agent
		Constants.USER_AGENT(this); 
		if (!ContextUtils.isOnline(this)) {
			showScreen();
			return;
		}
		username = ShareUtils.getPrefs(ShareUtils.USERNAME, this);
		password = ShareUtils.getPrefs(ShareUtils.PASSWROR, this);
		Log.i(TAG, "username:" + username + ", password:" + password);
		if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
			new Handler().postDelayed(new Runnable(){
			    public void run() {
			    	Log.i(TAG, "延迟500ms执行。");
					if (!ContextUtils.isServiceRunning(getBaseContext(), SubscribeService.class.getName())) {
						getBaseContext().sendBroadcast(new Intent(Constants.BROADCAST_SERVICE_ACTION));//如果没有运行，则启动服务
					}
			    }
			 }, 500);
			new AsyncTask<String, Void, String>() {
				@Override
				protected String doInBackground(String... params) {
					if (isCancelled()) {
						return null;
					}
					if (null == params || params.length == 0) {
						return null;
					}
					return HttpUtils.getIdCode(params[0]);
				}
				@Override
				protected void onPostExecute(String result) {
					Log.i(TAG, "Identify Code:" + result);
					if (null != result) {
						task = new AutoLoginDataTask(getContext()) {
							@Override
							public void loginOk(JSONObject userMap) {
//								Toast.makeText(context, "自动登录成功", Toast.LENGTH_LONG).show();
								startApp();
							}
							@Override
							public void loginFailed(String msg) {
								Toast.makeText(context, "自动登录失败", Toast.LENGTH_LONG).show();
								startApp();
							}
						};
						task.execute(username, password, result);
					} else {
						showScreen();;
					}
				}
			}.execute(Constants.APP_URL + "/login/mainJson.html");
		} else {
			showScreen();
		}
	}
	
	public Context getContext() {
		return this;
	}
	
	protected void showScreen() {
		showScreen(SPLASH_TIME_OUT);
	}
	
	protected void showScreen(int timeout) {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				startApp();
			}
		}, timeout);
	}
	
	protected void startApp() {
		Log.i(TAG, "startApp");
		Intent i = new Intent(SplashScreenActivity.this, GuideActivity.class);
		startActivity(i);
		// close this activity
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
}
